package pcjavanet.mobilesell.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pcjavanet.mobilesell.model.ExcelCustomer;
import pcjavanet.mobilesell.model.ExcelCustomerExample;
import pcjavanet.mobilesell.model.ExeclMobile;
import pcjavanet.mobilesell.model.ExeclMobileExample;
import pcjavanet.mobilesell.model.PageBean;
import pcjavanet.mobilesell.model.QryCondition;
import pcjavanet.mobilesell.persistence.ExcelCustomerMapper;
import pcjavanet.mobilesell.persistence.ExeclMobileMapper;


@Service
public class ExeclMobileService {


  @Autowired
  private  ExeclMobileMapper execlMobileMapper;
  

  @Autowired
  private  ExcelCustomerMapper excelCustomerMapper;


  public int  insert( ExeclMobile execlMobile ) throws Exception{
	return execlMobileMapper.insert (execlMobile);
  }


  public ExeclMobile  selectByPrimaryKey( ExeclMobile  bean ) {
 	int dbid = bean.getDbid();
	ExeclMobile execlMobile = execlMobileMapper.selectByPrimaryKey( dbid );
	return execlMobile ;
  }


  public int  updateByPrimaryKeySelective( ExeclMobile execlMobile ) throws Exception{
	return execlMobileMapper.updateByPrimaryKeySelective( execlMobile );
  }


  public int  deleteByPrimaryKey( ExeclMobile  bean ) throws Exception{
 	int dbid = bean.getDbid();
	return execlMobileMapper.deleteByPrimaryKey( dbid );
  }


  public List<ExeclMobile>  selectByExample( ExeclMobile execlMobile ) throws Exception{
    ExeclMobileExample example = new ExeclMobileExample();
    ExeclMobileExample.Criteria c = example.createCriteria();
    if (  execlMobile.getBuydate() != null )
    	c.andBuydateEqualTo( execlMobile.getBuydate());
    c.andCustomeridEqualTo(  execlMobile.getCustomerid() );
    c.andItemtypeEqualTo(  execlMobile.getItemtype() );
	return execlMobileMapper.selectByExample( example );
  }
  
  public List<ExeclMobile>  selectByExampleForMobile( ExeclMobile execlMobile ) throws Exception{
	  ExeclMobileExample example = new ExeclMobileExample();
	  ExeclMobileExample.Criteria c = example.createCriteria();
	  if (  execlMobile.getBuydate() != null )
		  c.andBuydateEqualTo( execlMobile.getBuydate());
	  
	  c.andSerialEqualTo( execlMobile.getSerial() );
	  c.andCustomeridEqualTo(  execlMobile.getCustomerid() );
	  return execlMobileMapper.selectByExample( example );
  }

  public List<ExeclMobile>  getOnePageBeans( QryCondition condition , PageBean pageBean ) throws Exception{
    ExeclMobileExample example = new ExeclMobileExample();
    ServiceUtil.dealCriteria( condition,  example.createCriteria() );
	List<ExeclMobile> ls=execlMobileMapper.getOnePageBeans( example , pageBean );
 
	List<Integer> mobiles = new ArrayList<Integer>();
	for(int i=0;i<ls.size();i++){
		if ( ls.get(i).getCustomerid() != null )
			mobiles.add( ls.get(i).getCustomerid());
	}
    ExcelCustomerExample example2 = new ExcelCustomerExample();
    ExcelCustomerExample.Criteria c =example2.createCriteria();
    c.andDbidIn(mobiles);
	List<ExcelCustomer> ls2=excelCustomerMapper.selectByExample(example2);
	
	for(int i= 0; i < ls.size() ; i++){
		for(int j=0;j<ls2.size() ; j++){
			if (ls.get(i).getCustomerid() !=null && ls2.get(j).getDbid()!=null &&  
					ls.get(i).getCustomerid().intValue() == ls2.get(j).getDbid().intValue()) {
				ls.get(i).setName(ls2.get(j).getName());
				ls.get(i).setMobile(ls2.get(j).getMobile());
			}
		}
	}
	
//begin
	if ( ls.size() > 0 ) {
	}
//end
	return ls;
  }
  public List<ExeclMobile>  getTopBeans( QryCondition condition , PageBean pageBean,boolean displayRecord ) throws Exception{
	  ExeclMobileExample example = new ExeclMobileExample();
	  ServiceUtil.dealCriteria( condition,  example.createCriteria() );
	  List<ExeclMobile> ls=execlMobileMapper.getTopBeans( example,pageBean  );
	  if ( ls.size() > 0 ) {
	  List<Integer> mobiles = new ArrayList<Integer>();
	  for(int i=0;i<ls.size();i++){
		  mobiles.add( ls.get(i).getCustomerid());
	  }
	  ExcelCustomerExample example2 = new ExcelCustomerExample();
	  ExcelCustomerExample.Criteria c =example2.createCriteria();
	  c.andDbidIn(mobiles);
	  List<ExcelCustomer> ls2=excelCustomerMapper.selectByExample(example2);
	  
	  for(int i= 0; i < ls.size() ; i++){
		  for(int j=0;j<ls2.size() ; j++){
			  if ( ls.get(i).getCustomerid().intValue() == ls2.get(j).getDbid().intValue()) {
				  ls.get(i).setName(ls2.get(j).getName());
				  ls.get(i).setMobile(ls2.get(j).getMobile());
			  }
		  }
	  }
	  }
//begin
	  if ( ls.size() > 0 ) {
	  }
//end
	  List<ExeclMobile> newLs = new ArrayList<ExeclMobile>();
	  if ( displayRecord == false ){
		   int index =0 ; 
		   boolean find = true ; 
		   ExeclMobile m = null;
		   while( ls.size()>0 && index<=ls.size() ){
			   if ( m==null || find )
				   m =ls.get(index);
			   if ( (index+1)<ls.size()){
				   if (m.getCustomerid().intValue() == ls.get(index+1).getCustomerid().intValue()) {
					   find = false ; 
				   } else {
					   find = true ; 
				   }
			   }
			  
			   if ( find )  
				   newLs.add(m);
			   index=index+1;
		   }
		   ls.clear();
		   ls.addAll(newLs);
	  }
	  return ls;
  }

  public int getTotalRecords( QryCondition condition , PageBean pageBean ) throws Exception{
    ExeclMobileExample example = new ExeclMobileExample();
    ServiceUtil.dealCriteria( condition,  example.createCriteria() );
	return execlMobileMapper.getTotalRecords( example , pageBean );
  }

  public int countByExample( ExeclMobile execlMobile , PageBean pageBean ){
    ExeclMobileExample example = new ExeclMobileExample();
	return execlMobileMapper.countByExample( example );
  }
  public List<ExeclMobile> selectByExamplePage( ExeclMobile execlMobile , PageBean pageBean ){
  ExeclMobileExample example = new ExeclMobileExample();
	return execlMobileMapper.selectByExamplePage( example , pageBean );
  }

}
